<?php
/**
 * “应用”辅助函数
 *
 * @author hiscaler <hiscaler@gmail.com>
 * @link http://www.yuandot.com/
 * @copyright Copyright &copy; 2009-2010 Yuandot Information Technology CO.,LTD.
 * @license http://doit.yuandot.com/license/
 */

class YApp {

    /**
     * 根据 URL 参数生成导航栏数据
     * @param string $caption
     * @return array
     */
    public static function generateAutoBreadcrumbs($caption = '') {
        $res = array();
        $has_fix = false;
        if (is_string($caption) && !empty($caption)) {
            $res[] = trim($caption);
            $has_fix = true;
        }
        $current_node = Yii::app()->request->getParam('node', 0);
        if ($current_node == 0) {
            return $res;
        } else {
            $cache_key = 'f_breadcrumbs_' . md5($caption . $current_node);
            $cache = Yii::app()->cache->get($cache_key);
            if ($cache === false) {
                $items = Node::getNodeInfos($current_node);
                foreach ($items as $item) {
                    if ($has_fix) {
                        $res[$item['name']] = array($item['url_args_frontend'], 'node' => $item['id']);
                    } else {
                        if ($current_node == $item['id']) {
                            $res[] = $item['name'];
                        } else {
                            $res[$item['name']] = array($item['url_args_frontend'], 'node' => $item['id']);
                        }
                    }
                }
            } else {
                return $cache;
            }
        }
        $r = array_reverse($res);
        Yii::app()->cache->set($cache_key, $r, 300);
        return $r;
    }
}
